package mp1.Controller;


import io.swagger.annotations.ApiOperation;
import mp1.entity.Product;
import mp1.entity.Supplier;
import mp1.exception.ProductAdminException;
import mp1.service.ProductService;
import mp1.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("supplier")
public class SupplierController {
    @Autowired
    SupplierService supplierService;

    @ApiOperation("根据Id查询商品")
    @GetMapping("/{id}")
    public ResponseEntity<Supplier> getProduct(@PathVariable long id){
        Supplier result = supplierService.findSupById(id);
        if(result==null) {
            return ResponseEntity.noContent().build();
        }else{
            return ResponseEntity.ok(result);
        }
    }


    @ApiOperation("根据Product查询供应商")
    @GetMapping("")
    public ResponseEntity<List<Supplier>> getProductByProName(@RequestBody String proName){
        List<Supplier> result = supplierService.findSupByPro(proName);
        if(result==null) {
            return ResponseEntity.noContent().build();
        }else{
            return ResponseEntity.ok(result);
        }
    }


    @GetMapping("/{id}")
    @DeleteMapping
    public void deleteProduct(@PathVariable long id) throws ProductAdminException {
        supplierService.deleteSupById(id);
    }

    @ApiOperation("添加供应商")
    @PostMapping("")
    public void addSupplier(@RequestBody Supplier supplier) throws ProductAdminException {
        supplierService.addSup(supplier);
    }

    @ApiOperation("修改供应商信息")
    @PutMapping("/{id}")
    public void updateSupplier(@RequestBody Supplier supplier) throws ProductAdminException {
        supplierService.updateSup(supplier);
    }
}
